package com.lianzhihui.romeex.mvp.model;

import android.content.Context;
import com.lianzhihui.romeex.bean.user.CountryCode;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.net.APIConstant;
import com.lianzhihui.romeex.net.HttpManager;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.net.HttpManagerCallback;
import com.lianzhihui.romeex.net.ResultData;
import com.lianzhihui.romeex.utils.DateUtil;
import com.lianzhihui.romeex.utils.GsonUtil;
import com.lianzhihui.romeex.utils.LogUtil;
import com.lianzhihui.romeex.utils.MyToast;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;
import java.util.List;

//网络数据请求实现
public class LoginModelImp {
    private Context context;
    LoginModelInterface loginModelInterface;
    public LoginModelImp(Context context,LoginModelInterface loginModelInterface){
        this.context = context;
        this.loginModelInterface = loginModelInterface;
    }

    public void createWallet() {
        loginModelInterface.onLoadCreateSuccess(new WalletInfo());
    }

    public void importWallet() {
        loginModelInterface.onLoadImportSuccess(new WalletInfo());
    }

    public void setWalletInfo(WalletInfo object){
        loginModelInterface.onSetWalletSuccess(object);
    }
    public void syncWalletInfo(WalletInfo walletInfo) {

        HttpParams params = new HttpParams();
        params.put("address",walletInfo.getAddress());
        params.put("addtime", System.currentTimeMillis()+"");
        new HttpManager(context, new HttpManagerCallback() {
            @Override
            public void onSuccess(ResultData o) {
                LogUtil.info("syncWalletInfo:",o.getCode()+"");
                if (o.getCode()==200) {
                    loginModelInterface.onSyncWalletSuccess();
                }else {
                    loginModelInterface.onSyncWalletFailed(o.getCode());
                }
            }

            @Override
            public void onError(ApiException e) {
                //需要验证手机号
                loginModelInterface.onSyncWalletFailed(e.getCode());
            }
        }).post(APIConstant.SYNC_WALLET,params);
    }
    public void getCountryCodeList() {
        HttpParams params = new HttpParams();
        new HttpManager(context, new HttpManagerCallback() {
            @Override
            public void onSuccess(ResultData o) {
                if (o.getCode()==200){
                    List<CountryCode> countryCodeList = GsonUtil.GsonToList(o.getData(),CountryCode.class);
                    loginModelInterface.onLoadReaginSuccess(countryCodeList);
                }else {
                    loginModelInterface.onFailure();
                }
            }

            @Override
            public void onError(ApiException e) {
                loginModelInterface.onFailure();
            }
        }).post(APIConstant.REGION_LIST,params);
    }
    public void getPhoneCode(String phoneNumber,String countryCode) {
        HttpParams params = new HttpParams();
        params.put("phoneNumber",phoneNumber);
        params.put("countryCode",countryCode);
        new HttpManager(context, new HttpManagerCallback() {
            @Override
            public void onSuccess(ResultData o) {
                if (o.getCode()==200){
                    loginModelInterface.onGetPhoneCodeSuccess(o.getData().getAsString());
                }else if (o.getCode()==210003){
                    MyToast.showToast(context.getResources().getString(R.string.phone_has_bind));
                    loginModelInterface.onFailure();
                }
            }

            @Override
            public void onError(ApiException e) {
                loginModelInterface.onFailure();
            }
        }).post(APIConstant.GET_PHONE_CODE,params);
    }
    public void syncPhoneCode(String phone,String verCode,String areaCode) {
        HttpParams params = new HttpParams();
        params.put("phone",phone);
        params.put("verCode",verCode);
        params.put("phone_code",areaCode);
        new HttpManager(context, new HttpManagerCallback() {
            @Override
            public void onSuccess(ResultData o) {
                if (o.getCode()==200){
                    loginModelInterface.onSyncPhoneCodeSuccess(o.getData().getAsString());
                }else {
                    MyToast.showToast(o.getData().getAsString());
                    loginModelInterface.onFailure();
                }
            }

            @Override
            public void onError(ApiException e) {
                MyToast.showToast(e.getMessage());
                loginModelInterface.onFailure();
            }
        }).post(APIConstant.IMPORT_PHONE,params);
    }
}
